<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<%
if not CheckFlag(session("aaPostFlag"),1) then call JumpErrorPage(2)
export=trim(SafeRequest("export",0))
if export="xls" then
    Response.ContentType ="application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition","attachment; filename=excel.xls"
end if
%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>报关单登记系统</title>
<!--#include file="lib/js-jquery-lib.asp"-->
<script type="text/javascript" src="lib/jquery.printElement.min.js"></script>
<script type="text/javascript">
    $(function() {
    /*
        // set portrait orientation
        //jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
        jsPrintSetup.setOption('orientation', jsPrintSetup.kLandscapeOrientation);
        // set top margins in millimeters
        jsPrintSetup.setOption('marginTop', 15);
        jsPrintSetup.setOption('marginBottom', 15);
        jsPrintSetup.setOption('marginLeft', 10);
        jsPrintSetup.setOption('marginRight', 10);
        // set page header
        jsPrintSetup.setOption('headerStrLeft', '');
        jsPrintSetup.setOption('headerStrCenter', '');
        jsPrintSetup.setOption('headerStrRight', '');
        // set empty page footer
        jsPrintSetup.setOption('footerStrLeft', '');
        jsPrintSetup.setOption('footerStrCenter', '&PT');
        jsPrintSetup.setOption('footerStrRight', '');
        jsPrintSetup.setOption('shrinkToFit', 1);
        jsPrintSetup.setOption('paperData','DMPAPER_A4');
        // clears user preferences always silent print value
        // to enable using 'printSilent' option
        jsPrintSetup.clearSilentPrint();
        // Suppress print dialog (for this context only)
        jsPrintSetup.setOption('printSilent', 1);
        // Do Print 
        // When print is submitted it is executed asynchronous and
        // script flow continues after print independently of completetion of print process! 
        //jsPrintSetup.print();
        // next commands

    */
    
        $('body').printElement();
    });
</script>
<!--#include file="lib/js-google-analytics.asp"-->
</head>
<%

'获取提交查询的参数，和search_main一样
tx_search=trim(SafeRequest("tx_search",0))
wtr=trim(SafeRequest("wtr",1))
bwtr=trim(SafeRequest("bwtr",1))
category=trim(SafeRequest("category",1))
cd_status=trim(SafeRequest("cd_status",1))
fee=trim(SafeRequest("fee",0))
passno=trim(SafeRequest("passno",0))
orderno=trim(SafeRequest("orderno",0))
boxno=trim(SafeRequest("boxno",0))
ksrq=trim(SafeRequest("ksrq",0))
if ksrq<>"" then ksrq=cdate(ksrq)
jsrq=trim(SafeRequest("jsrq",0))
if jsrq<>"" then jsrq=cdate(jsrq)
jsksrq=trim(SafeRequest("jsksrq",0))
if jsksrq<>"" then jsksrq=cdate(jsksrq)
jsjsrq=trim(SafeRequest("jsjsrq",0))
if jsjsrq<>"" then jsjsrq=cdate(jsjsrq)
ytksrq=trim(SafeRequest("ytksrq",0))
if ytksrq<>"" then ytksrq=cdate(ytksrq)
ytjsrq=trim(SafeRequest("ytjsrq",0))
if ytjsrq<>"" then ytjsrq=cdate(ytjsrq)
cd_received=trim(SafeRequest("cd_received",0))
cd_paid=trim(SafeRequest("cd_paid",0))
cd_confirm=trim(SafeRequest("cd_confirm",0))
cd_check=trim(SafeRequest("cd_check",0))
post=SafeRequest("post",1)
detail=SafeRequest("detail",1)
search_count=trim(SafeRequest("search_count",1))
'额外打印需要的参数
print_title=trim(SafeRequest("print_title",0))
search_col=saferequest("search_col",0)
'response.write(search_col)
print_mode=saferequest("print_mode",1)

cd_synmode=SafeRequest("cd_synmode",1)
cd_source=SafeRequest("cd_source",1)
duplicate=trim(SafeRequest("duplicate",0))
dept=trim(SafeRequest("dept",1))
ic=trim(SafeRequest("ic",0))
input_ic=trim(SafeRequest("input_ic",0))
roleid=trim(SafeRequest("roleid",1))
userid=trim(SafeRequest("userid",1))
edoc=trim(SafeRequest("edoc",0))
entry_type=trim(SafeRequest("entry_type",0))
cd_hidden=trim(SafeRequest("cd_hidden",0))

' response.write(search_col)
'需要打印的列
aCol=split(search_col,", ")
' dim sum()
' redim sum(ubound(aCol))
iColCount=ubound(aCol)

Set dDetail=Server.CreateObject("Scripting.Dictionary")


'用来存储要进行合计的数组
for iLoop=0 to ubound(aCol)
    ' response.write(aCol(iLoop)&"<br />")
    if left(aCol(iLoop),7)="detail_" then
        aDetail=split(aCol(iLoop),"|")
        dDetail.add aDetail(0),aDetail(1)
        iColCount=iColCount-1
    end if
next
dDetailKeys=dDetail.keys()
dDetailVals=dDetail.items()
for iLoop=0 to dDetail.count-1
    ' response.write(dDetailKeys(iLoop)&":"&dDetailVals(iLoop)&"<br />")
next


'组合sql查询字符串，和search_main一样
'sql="select * from cdfee where true "
sql="select "
if search_count<>0 then sql=sql&" top "&search_count
sql=sql&" * from cdfee where true "
if tx_search<>"" then
    if len(tx_search)=18 and IsNumeric(tx_search) then
        '如果是18位的报关单号，直接查询。用于扫描枪的使用，提高搜索速度
        sql=sql&" and cd_no='"&tx_search&"'"
    else
        sql=sql&" and (cd_no like '%"&tx_search&"%' or cd_company like '%"&tx_search&"%' or cd_voyage like '%"&tx_search&"%' or cd_lading like '%"&tx_search&"%' or cd_verify like '%"&tx_search&"%' or cd_passno like '%"&tx_search&"%' or cd_orderno like '%"&tx_search&"%' or cd_boxno like '%"&tx_search&"%' or cd_memo like '%"&tx_search&"%')"
    end if
end if
if CheckWtr() then
  show_rp="true"
  sql=sql&" and wtr="&session("iWtr")
else
  if wtr<>0 then sql=sql&" and wtr="&wtr
  if bwtr<>0 then sql=sql&" and bwtr="&bwtr
end if
if category<>0 then sql=sql&" and category="&category
if cd_status<>0 then sql=sql&" and cd_status="&cd_status
if ksrq<>"" and jsrq<>"" then    sql=sql&" and (cd_date>=#"&ksrq&"# and cd_date<=#"&jsrq&"#)"
if jsksrq<>"" and jsjsrq<>"" then    sql=sql&" and (cd_settledate>=#"&jsksrq&"# and cd_settledate<=#"&jsjsrq&"#)"
if ytksrq<>"" and ytjsrq<>"" then    sql=sql&" and (cd_statusdate>=#"&ytksrq&"# and cd_statusdate<=#"&ytjsrq&"#)"
if cd_received<>"" then sql=sql&" and cd_received="&cd_received
if cd_paid<>"" then sql=sql&" and cd_paid="&cd_paid
if cd_confirm<>"" then sql=sql&" and cd_confirm="&cd_confirm
if cd_check<>"" then sql=sql&" and cd_check="&cd_check
if post=0 then
'如果不选择要查询的岗位，则查询该用户所有的有查询权限的岗位
    sSearchPost=""
    for i=0 to ubound(session("aaPostFlag"),2)
        if session("aaPostFlag")(1,i)>=1 and session("aaPostFlag")(0,i)<>0 and session("aaPostFlag")(0,i)<>5 then
            sSearchPost=sSearchPost&session("aaPostFlag")(0,i)&","
        end if
    next
    if len(sSearchPost)>0 then
        sSearchPost=left(sSearchPost,len(sSearchPost)-1)
        sql=sql&" and cd_post in ("&sSearchPost&")"
    else
        sql=sql&" and cd_post=-1"
    end if
else
'如果选择了查询岗位，则只查询该岗位
    sql=sql&" and cd_post="&post
end if
'查询详细费用的类别，判断是否有该类别的费用产生，返回cd
if detail<>0 then
    set rs_detail=Server.CreateObject("ADODB.Recordset")
    sql_detail="select DISTINCT cd from feedetail where feecategory.id="&detail
    rs_detail.open sql_detail,conn,1,1
    if not rs_detail.eof then
        '如果找到该类别的详细费用，就组合字段
        detail_querystring=rs_detail.getstring(,,,",")
    else
        '如果没找到该类别的详细费用，就用0来查询
        detail_querystring="0"
    end if
    rs_detail.close
    set rs_detail=nothing
    sql=sql&" and id in ("&detail_querystring&")"
end if
if fee<>"" then
    if fee then    sql=sql&" and not isnull(cd)" else sql=sql&" and isnull(cd)"
end if
if passno<>"" then
    if passno then sql=sql&" and cd_passno<>''" else sql=sql&" and cd_passno=''"
end if
if orderno<>"" then
    if orderno then sql=sql&" and cd_orderno<>''" else sql=sql&" and cd_orderno=''"
end if
if boxno<>"" then
    if boxno then sql=sql&" and cd_boxno<>''" else sql=sql&" and cd_boxno=''"
end if

if cd_synmode<>0 then sql=sql&" and cd_synmode="&cd_synmode
if cd_source<>0 then sql=sql&" and cd_source="&cd_source

if duplicate<>"" then
    set rs_duplicate=Server.CreateObject("ADODB.Recordset")
    sql_duplicate="select cd_no from cd group by cd_no having count(cd_no)>1"
    rs_duplicate.open sql_duplicate,conn,1,1
    if not rs_duplicate.eof then
        duplicate_querystring=rs_duplicate.getstring(,,,"','")
        duplicate_querystring="'"&duplicate_querystring&"'"
    else
        duplicate_querystring="''"
    end if
    rs_duplicate.close
    set rs_duplicate=nothing
    if duplicate then
        sql=sql&" and cd_no in ("&duplicate_querystring&")"
    else
        sql=sql&" and cd_no not in ("&duplicate_querystring&")"
    end if
end if
if dept<>0 then
    set rs_ic_code=Server.CreateObject("ADODB.Recordset")
    sql_ic_code="select ic_code from bwtric,deptic where deptic.icid=bwtric.id and deptic.deptid="&dept
    rs_ic_code.open sql_ic_code,conn,1,1
    if not rs_ic_code.eof then
        ic_code_querystring=rs_ic_code.getstring(,,,"','")
        ic_code_querystring="'"&ic_code_querystring&"'"
    else
        ic_code_querystring="''"
    end if
    rs_ic_code.close
    set rs_ic_code=nothing
    sql=sql&" and ic_code in ("&ic_code_querystring&")"
end if
if ic<>"" then sql=sql&" and ic_code='"&ic&"'"
if input_ic<>"" then sql=sql&" and input_iccode='"&input_ic&"'"
if roleid<>0 and userid<>0 then
    sql=sql&" and id in (select cdid from cdroleuser where roleid="&roleid&" and userid="&userid&")"
else
    if roleid<>0 then sql=sql&" and id in (select cdid from cdroleuser where roleid="&roleid&")"
    if userid<>0 then sql=sql&" and id in (select cdid from cdroleuser where userid="&userid&")"
end if
if edoc<>"" then 
    if edoc then
        sql=sql&" and id in (select cd from cdedoc)"
    else
        sql=sql&" and id not in (select cd from cdedoc)"
    end if
end if
if entry_type<>"" then
    sql=sql&" and cd_entrytype='"&entry_type&"'"
end if

if cd_hidden<>"" then sql=sql&" and cd_hidden="&cd_hidden


sql=sql&" order by cd_update desc,id desc"
'response.write sql
'response.end
'执行数据查询
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
If rs.eof Then
    response.write("没有检索到数据!")
    response.end
' else
    ' sql_sum="select sum(cd_receive) as sum_cd_receive,sum(cd_pay) as sum_cd_pay,sum(total_receive) as sum_total_receive,sum(total_pay) as sum_total_pay from("&sql&")"
    ' set rs_sum = Server.CreateObject("ADODB.Recordset")
    ' rs_sum.open sql_sum,conn,1,1
    ' sum_cd_receive=rs_sum("sum_cd_receive")
    ' sum_cd_pay=rs_sum("sum_cd_pay")
    ' sum_total_receive=rs_sum("sum_total_receive")
    ' sum_total_pay=rs_sum("sum_total_pay")
    ' rs_sum.close
    ' set rs_sum=nothing

End if
if export="xls" then 
    call SimpleRecord("cd","null","导出","导出-"&rs.RecordCount&"条",session("iId"),session("iClientId"),false)
else
    call SimpleRecord("cd","null","打印","打印-"&rs.RecordCount&"条",session("iId"),session("iClientId"),false)
end if


%>
<body style="font-size:10pt;font-family:宋体">
<div>
<h1 style="text-align:center"><%=print_title%></h1>
<%
    if print_mode=1 then
%>
    <table width="100%" border="1" style="border-collapse:collapse;">
        <thead>
            <tr>
                <th>序号</th>
                <%
                    for iLoop=0 to iColCount
                        response.write("<th scope='col'>"&d.item(aCol(iLoop))&"</th>")
                    next
                    for iLoop=0 to dDetail.Count-1
                        response.write("<th scope='col'>"&dDetailVals(iLoop)&"</th>")
                    next
                %>
            </tr>
        </thead>
        <tbody>
    <%
        set rs_fee = Server.CreateObject("ADODB.Recordset")
        do while not rs.eof
    %>
            <tr>
                <td align="center"><%=rs.AbsolutePosition%></td>
                <%for iLoop=0 to iColCount%>
                <td <%if t.item(aCol(iLoop)) then response.write("align='center'")%>>
                <%
                    select case aCol(iLoop)
                        case "cd_no"
                            '报关单号显示9位数字
                            if rs("cd_source")<>2 then
                                response.write(right(rs(aCol(iLoop)),9))
                            else
                                response.write(rs(aCol(iLoop)))
                            end if
                        case "cy_receive","sj_receive","jk_receive","qt_receive","cy_pay","sj_pay","jk_pay","qt_pay"
                            sql_fee="select sum("&right(aCol(iLoop),len(aCol(iLoop))-3)&") as "&aCol(iLoop)&" from feedetail where feecategory.short='"&left(aCol(iLoop),2)&"' and cd="&rs("id")
                            ' response.write(sql_fee)
                            'response.end
                            rs_fee.open sql_fee,conn,1,1
                                if rs_fee(aCol(iLoop))<>"" then
                                    response.write(rs_fee(aCol(iLoop)))
                                end if
                            rs_fee.close
                        case "cd_box"
                            Set rs_cdbox = Server.CreateObject("ADODB.Recordset")
                            sql_box = "SELECT top 1 box FROM cdbox where cd="&rs("id")
                            ' response.write(sql_box)
                            ' response.end
                            rs_cdbox.Open sql_box,conn,1,1
                            if not rs_cdbox.eof then response.write(rs_cdbox("box"))
                            rs_cdbox.close
                            set rs_cdbox=nothing
                        case "cd_curr"
                            Set rs_cdcurr = Server.CreateObject("ADODB.Recordset")
                            sql_curr = "SELECT curr_name FROM cd_curr inner join curr on cd_curr.curr=curr.curr_code where cd_curr.cd="&rs("id")
                            ' response.write(sql_curr)
                            ' response.end
                            rs_cdcurr.Open sql_curr,conn,1,1
                            if not rs_cdcurr.eof then response.write(rs_cdcurr("curr_name"))
                            rs_cdcurr.close
                            set rs_cdcurr=nothing
                        case else
                            response.write(rs(aCol(iLoop)))
                            
                    end select
                %>
                </td>
                <%next%>
                <%for iLoop=0 to dDetail.Count-1%>
                <td align="center">
                <%
                    aDetailKey=split(dDetailKeys(iLoop),"_")
                    sql_fee="select "&aDetailKey(2)&" from fee where detail="&aDetailKey(1)&" and cd="&rs("id")
                    ' response.write(sql_fee)
                    'response.end
                    rs_fee.open sql_fee,conn,1,1
                    if not rs_fee.eof then
                        ' if rs_fee(aDetailKey(2))<>"" then
                        response.write(rs_fee(aDetailKey(2)))
                    ' else
                        ' response.write("0")
                    end if
                    rs_fee.close
                %>
                </td>
                <%next%>
            </tr>
    <%
            rs.movenext
        loop
        set rs_fee=nothing
        set rs_sum = Server.CreateObject("ADODB.Recordset")
    %>
            <tr>
                <th>合计</th>
                <%for iLoop=0 to iColCount%>
                <th scope='col'>
                <%
                    if t.item(aCol(iLoop)) then
                        '判断是否需要显示合计
                        select case aCol(iLoop)
                            case "cd_receive","cd_pay","cd_actreceive","cd_actpay","total_receive","total_pay"
                                sql_sum="select sum("&aCol(iLoop)&") as sum_"&aCol(iLoop)&" from("&sql&")"
                                ' response.write(sql_sum)
                                rs_sum.open sql_sum,conn,1,1
                                response.write(rs_sum("sum_"&aCol(iLoop)))
                                rs_sum.close
                                ' set rs_sum=nothing
                            case "cy_receive","sj_receive","jk_receive","qt_receive","cy_pay","sj_pay","jk_pay","qt_pay"
                                sql_sum="select sum("&right(aCol(iLoop),len(aCol(iLoop))-3)&") as "&aCol(iLoop)&" from ("&sql&") as dd left join feedetail on dd.id=feedetail.cd where feecategory.short='"&left(aCol(iLoop),2)&"'"
                                ' set rs_sum = Server.CreateObject("ADODB.Recordset")
                                rs_sum.open sql_sum,conn,1,1
                                if rs_sum(aCol(iLoop))<>"" then
                                    response.write(rs_sum(aCol(iLoop)))
                                else
                                    response.write("0")
                                end if
                                rs_sum.close
                                ' set rs_sum=nothing
                        end select
                    end if
                %>
                </th>
                <%next%>
                <%for iLoop=0 to dDetail.Count-1%>
                <th scope='col'>
                <%
                    aDetailKey=split(dDetailKeys(iLoop),"_")
                    sql_sum="select sum("&aDetailKey(2)&") as sum_"&aDetailKey(2)&" from ("&sql&") as cdfee,fee where cdfee.id=fee.cd and detail="&aDetailKey(1)
                    ' response.write(sql_sum)
                    'response.end
                    rs_sum.open sql_sum,conn,1,1
                    if not rs_sum.eof then
                        response.write(rs_sum("sum_"&aDetailKey(2)))
                    end if
                    rs_sum.close
               %>
                </th>
                <%next%>
            </tr>
        </tbody>
    </table>
    <%
         set rs_sum=nothing
    %>
<%
    end if
    if print_mode=2 then
%>
    <table width="100%" border="1" style="border-collapse:collapse;">
        <thead>
            <tr>
                <th>序号</th>
                <%
                    for iLoop=0 to iColCount
                        if instr(aCol(iLoop),"receive")=0 and instr(aCol(iLoop),"pay")=0 then
                            response.write("<th scope='col'>"&d.item(aCol(iLoop))&"</th>")
                        end if
                    next
                %>
                <th scope="col">费用</th>
                <th scope="col">金额</th>
            </tr>
        </thead>
        <tbody>
            <%
                do while not rs.eof
            %>
            <!-- 先打印报关费 -->
            <tr>
                <td align="center"><%=rs.AbsolutePosition%></td>
                <%
                for iLoop=0 to iColCount
                    if instr(aCol(iLoop),"receive")=0 and instr(aCol(iLoop),"pay")=0 then
                %>
                
                <td <%if t.item(aCol(iLoop)) then response.write("align='center'")%>>
                <%
                    if aCol(iLoop)="cd_no" and rs("cd_source")<>2 then 
                        '报关单号显示9位数字
                        response.write(right(rs(aCol(iLoop)),9))
                    elseif aCol(iLoop)="cd_box" then
                        Set rs_cdbox = Server.CreateObject("ADODB.Recordset")
                        sql_box = "SELECT top 1 box FROM cdbox where cd="&rs("id")
                        ' response.write(sql_box)
                        ' response.end
                        rs_cdbox.Open sql_box,conn,1,1
                        if not rs_cdbox.eof then response.write(rs_cdbox("box"))
                        rs_cdbox.close
                        set rs_cdbox=nothing
                    elseif aCol(iLoop)="cd_curr" then
                        Set rs_cdcurr = Server.CreateObject("ADODB.Recordset")
                        sql_curr = "SELECT curr_name FROM cd_curr inner join curr on cd_curr.curr=curr.curr_code where cd_curr.cd="&rs("id")
                        ' response.write(sql_curr)
                        ' response.end
                        rs_cdcurr.Open sql_curr,conn,1,1
                        if not rs_cdcurr.eof then response.write(rs_cdcurr("curr_name"))
                        rs_cdcurr.close
                        set rs_cdcurr=nothing
                    else
                        response.write(rs(aCol(iLoop)))
                    end if
                %>
                </td>
                <%
                    end if
                next
                %>
                <td>报关费</td>
                <td><%=rs("cd_receive")%></td>
            </tr>
            <!-- 再打印详细费用 -->
            <%
            
                
                    set rs_fee = Server.CreateObject("ADODB.Recordset")
                    sql_fee="select * from feedetail where cd="&rs("id")
                    rs_fee.open sql_fee,conn,1,1
                    do while not rs_fee.eof

                        if rs_fee("receive")<>0 then
            %>
            <tr>
                <td align="center"><%=rs.AbsolutePosition%></td>
                <%
                for iLoop=0 to iColCount
                    if instr(aCol(iLoop),"receive")=0 and instr(aCol(iLoop),"pay")=0 then
                %>
                <td <%if t.item(aCol(iLoop)) then response.write("align='center'")%>>
                <%
                    if aCol(iLoop)="cd_no" and rs("cd_source")<>2 then 
                        '报关单号显示9位数字
                        response.write(right(rs(aCol(iLoop)),9))
                    elseif aCol(iLoop)="cd_box" then
                        Set rs_cdbox = Server.CreateObject("ADODB.Recordset")
                        sql_box = "SELECT top 1 box FROM cdbox where cd="&rs("id")
                        ' response.write(sql_box)
                        ' response.end
                        rs_cdbox.Open sql_box,conn,1,1
                        if not rs_cdbox.eof then response.write(rs_cdbox("box"))
                        rs_cdbox.close
                        set rs_cdbox=nothing
                    elseif aCol(iLoop)="cd_curr" then
                        Set rs_cdcurr = Server.CreateObject("ADODB.Recordset")
                        sql_curr = "SELECT curr_name FROM cd_curr inner join curr on cd_curr.curr=curr.curr_code where cd_curr.cd="&rs("id")
                        ' response.write(sql_curr)
                        ' response.end
                        rs_cdcurr.Open sql_curr,conn,1,1
                        if not rs_cdcurr.eof then response.write(rs_cdcurr("curr_name"))
                        rs_cdcurr.close
                        set rs_cdcurr=nothing
                    else
                        response.write(rs(aCol(iLoop)))
                    end if
                %>
                </td>
                <%
                    end if
                next
                %>
                <td><%=rs_fee("feename")%></td>
                <td><%=rs_fee("receive")%></td>
            </tr>
            <%
                        rs_fee.movenext
                        end if
                    loop
                    set rs_fee=nothing
                    rs.movenext
                loop
            %>
            <!-- 最后打印合计 -->
            <tr>
            </tr>
        </tbody>
    </table>
<%
    end if
%>
<%
rs.close
Set rs=nothing
set dDetail=nothing
%>
</div>
</body>
</html>
<!--#include file="lib/connclose.asp"-->

